package com.woniuxy.automotivemanagementsystem.controller;

import com.woniuxy.automotivemanagementsystem.entity.Menu;
import com.woniuxy.automotivemanagementsystem.service.MenuService;
import com.woniuxy.automotivemanagementsystem.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/menu")
@CrossOrigin
public class MenuController {
    @Autowired
    private MenuService menuService;
    @GetMapping("/queryMenuByUid/{uid}")
    public Result queryMenuByUid(@PathVariable Integer uid){
        List<Menu> menus = menuService.queryMenuByUid(uid);
        return Result.success(menus);
    }
    /*
    * 查询所有菜单数据，角色管理中分配菜单时使用
    * */
    @GetMapping("/queryAllofTree")
    public Result queryAllofTree(){
        //service调用查询所有方法，需要树形结构的数据
        List<Menu> menus = menuService.queryAllofTree();
        return Result.success(menus);
    }
    /*
    * 新增菜单接口
    * */
    @PostMapping("/addOrUpdate")
    public Result addOrUpdate(@RequestBody Menu menu){
        //判断是新增一级菜单还是二级菜单还是修改菜单
        if(menu.getPid() == null || menu.getPid() == 0){
            menu.setPid(0);
            menu.setCreatetime(new Date());
            menu.setUpdatetime(new Date());
            menuService.saveOrUpdate(menu);
        }else{
            menu.setCreatetime(new Date());
            menu.setUpdatetime(new Date());
            menuService.saveOrUpdate(menu);
        }
        return Result.success(true);
    }
    /*
    *修改菜单接口
    * */
    @PutMapping("/updateMenu")
    public Result updateMenu(@RequestBody Menu menu){
        menu.setUpdatetime(new Date());
        menuService.updateById(menu);
        return Result.success(true);
    }
    /*
    * 删除菜单接口
    * */
    @DeleteMapping("/delectMenu/{mid}")
    public Result delectMenu(@PathVariable Integer mid){
        return menuService.removeMenuByMid(mid);
    }
}
